const CreateStation = require('./CreateStation');
const { CesiumFun } = require('./CesiumFun');
class SerJSONAdd {
    constructor() {

    }
    /**
     * @description 自动监测站数据可视化
     * @author tpy
     * @annotator hjj
     * @date 2020-05-26
     * @param {String} starttm 开始时间
     * @param {String} endtm 结束时间
     * @param {Function} callback 回调函数
     * @returns
     * @memberof SerJSONAdd
     */
    StationInit (callback) {
        try {
            let tablelayer = new Object();//存储数据库表名和测站类型的映射关系
                tablelayer.all = [];
                let layersENCH = [];//存储测站的中英文映射关系
                let datas = window.stationData.data;
                //console.log("自动监测站数据结构", JSON.stringify(datas));
                let allObj = new Object();
                for (let i = 0; i < datas.length; i++) {
                    let stalayer = datas[i];
                    let keys = Object.keys(stalayer);
                    for (let ki = 0; ki < keys.length; ki++) {
                        if (keys[ki].indexOf("-CN") === -1) {//过滤掉含有"-CN"的Key
                            tablelayer.all = [...tablelayer.all, keys[ki]];
                            let layerENCH = new Object();
                            layerENCH.ENName = keys[ki];//英文名称 
                            layerENCH.CHName = stalayer[keys[ki] + "-CN"];//中文名称
                            layerENCH.index = window.CesiumViewer.dataSources.length;
                            layersENCH.push(layerENCH);
                            try {
                                let tempobj = this._CreateStaionLayer(keys[ki], stalayer[keys[ki]]);//创建测站实体
                                allObj = { ...allObj, ...tempobj };
                            } catch (e) {
                                console.log(layerENCH, stalayer[keys[ki]]);
                            }
                            //存储数据库表名和测站类型的映射关系
                            if (keys[ki] === "DDeng") {
                                tablelayer["st_was_r"] = ["DDeng"];//堰闸水情
                            } else if (keys[ki] === "DPeng") {
                                tablelayer["st_pump_r"] = ["DPeng"];//泵站水情
                            } else if (keys[ki] === "HP") {
                                tablelayer["st_rsvr_r"] = ["HP"];//水库水情
                            }
                            else if (stalayer[keys[ki]].length > 0) {
                                for (let t = 0; t < stalayer[keys[ki]].length; t++) {
                                    let tempst = stalayer[keys[ki]][t].Value;
                                    if (typeof (tempst) !== "undefined") {
                                        let stkeys = Object.keys(tempst);
                                        for (let stkey of stkeys.keys()) {
                                            if (typeof (tablelayer[stkeys[stkey]]) === "undefined") {
                                                let templayers = [];
                                                templayers.push(keys[ki]);
                                                tablelayer[stkeys[stkey]] = templayers;
                                            } else {
                                                let templayers = tablelayer[stkeys[stkey]];
                                                // templayers.push(keys[ki]);
                                                if (templayers.indexOf(keys[ki]) == -1) {
                                                    templayers.push(keys[ki]);
                                                }
                                                tablelayer[stkeys[stkey]] = templayers;
                                            }
                                        }
                                    }
                                }
                            }
                        } else {

                        }
                    }
                }
                if (typeof (callback) === "function") {
                    callback({ layersENCH: layersENCH, tablelayer: tablelayer, AllObj: allObj });
                }
        } catch (e) {
            console.log("测站初始化失败：", e);
        }

    }


    _NameInTree = (name, treeArr) => {
        try {
            for (let i = 0; i < treeArr.length; i++) {
                if (name.name == treeArr[i].name) {
                    return i;
                }
            }
        } catch (e) {
            console.log("图层名搜索出错:", e);
        }
        return -1;
    }
    /**
     * @description 创建自动测站图层，按测站类型分类
     * @author tpy
     * @annotator hjj
     * @date 2020-05-26
     * @param {String} layername 测站类型名称，e.g. MM、PIMS、RIWQ……
     * @param {Array} datas 多个同类型测站的监测数据
     * @returns {Object} Key-测站编码，Value-测站水质等级
     * @memberof SerJSONAdd
     */
    _CreateStaionLayer (layername, datas) {
        try {
            let pcesiumfun = new CesiumFun();
            let ds = pcesiumfun.dsgetbyname(layername);
            let tbobj = new Object();
            for (let i = 0; i < datas.length; i++) {
                if (layername === "DDeng" || layername === "DPeng") {//泵站
                    datas[i].baseInfo.stnm = layername == "DDeng" ? datas[i].baseInfo.gateName : datas[i].baseInfo.pumpName;//测站名称设置 
                    let dkeys = Object.keys(datas[i]);
                    datas[i].baseInfo.sttp = layername;//测站类型
                    datas[i].Value = new Object();
                    for (let s = 0; s < dkeys.length; s++) {
                        let dkey = dkeys[s];
                        if (dkey !== "baseInfo") {
                            datas[i].Value[dkey] = datas[i][dkey];//测站监测值存入Value中
                        }
                    }
                } else if (layername === "HP") {//电站
                    let dkeys = Object.keys(datas[i]);
                    datas[i].baseInfo.sttp = layername;
                    datas[i].Value = new Object();
                    for (let s = 0; s < dkeys.length; s++) {
                        let dkey = dkeys[s];
                        if (dkey !== "baseInfo") {
                            datas[i].Value[dkey] = datas[i][dkey];
                        }
                    }
                }
                let pCreateStation = new CreateStation();
                let itemgd = this._findTabWQV(datas[i].Value, "wq_awqmd_d");//计算水质综合等级
                if (itemgd > -1) {
                    tbobj[datas[i].baseInfo.stcd] = itemgd;
                }
                let station = pCreateStation.CreateStationEntity(layername, datas[i]);//创建测站实体
                if (station !== null && typeof (station) === "object") {
                    ds.entities.add(station);
                }
            }
            return tbobj;
        } catch (e) {
            console.log("测站图层创建失败：", e);
        }

    }

    /**
     * @description 计算水质综合等级
     * @author tpy
     * @annotator hjj
     * @date 2020-05-26
     * @param {Object} itemdata 监测数据
     * @param {string} tname 不同监测类型的数据库表名
     * @returns {Int32}
     * @memberof SerJSONAdd
     */
    _findTabWQV = (itemdata, tname) => {
        try {
            if (itemdata[tname]) {
                let tdata = itemdata[tname];
                if (tdata.gdhc && tdata.gddbs) {
                    let itemgd = tdata.gdhc > 1 ? 6 + (tdata.gdhc - 0.0) : tdata.gddbs - 0.0;
                    return itemgd;
                } else if (tdata.gddbs) {
                    return tdata.gddbs - 0.0;
                } else if (tdata.gdhc) {
                    return 6 + (tdata.gdhc - 0.0)
                } else {
                    return 0;
                }
            } else {
                return -1;
            }
        } catch (e) {
            console.log("", e);
        }

    }
}
export {SerJSONAdd};